package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.uuid.Seq;
import com.ruoyi.system.api.domain.File;
import com.ruoyi.system.domain.SysApp;
import com.ruoyi.system.mapper.SysAppMapper;
import com.ruoyi.system.mapper.SysAppVersionMapper;
import com.ruoyi.system.service.IFileService;
import com.ruoyi.system.service.ISysAppService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SysAppServiceImpl implements ISysAppService {

    @Autowired
    private SysAppMapper sysAppMapper;

    @Autowired
    private SysAppVersionMapper appVersionMapper;

    @Autowired
    private IFileService fileService;

    @Autowired
    private JdbcTemplate jdbcTemplate;


    @Override
    public SysApp selectSysAppById(Long id) {
        SysApp sysApp = sysAppMapper.selectSysAppById(id);
        // 39 应用图标
        List<File> iconList = fileService.selectFileListByGroupId(id, "39");
        if (StringUtils.isNotEmpty(iconList)) {
            sysApp.setIconList(iconList);
        }
        // 40 应用截图
        List<File> screenshotList = fileService.selectFileListByGroupId(id, "40");
        if (StringUtils.isNotEmpty(screenshotList)) {
            sysApp.setScreenshotList(screenshotList);
        }
        return sysApp;
    }

    @Override
    public List<SysApp> selectSysAppList(SysApp sysApp) {
        return sysAppMapper.selectSysAppList(sysApp);
    }

    @Override
    @Transactional
    public int insertSysApp(SysApp sysApp) {
        verfy(sysApp);
        return sysAppMapper.insertSysApp(sysApp);
    }

    @Override
    @Transactional
    public int updateSysApp(SysApp sysApp) {
        verfy(sysApp);
        return sysAppMapper.updateSysApp(sysApp);
    }

    /**
     * * 插入应用图标和应用截图
     * @param sysApp
     */
    private void verfy(SysApp sysApp) {
        Integer count;
        if (sysApp.getId() != null) {
            count = jdbcTemplate.queryForObject("select count(*) from sys_app where id <> ? and app_id =?", Integer.class, sysApp.getId(), sysApp.getAppId().trim());
            if (count > 0) {
                throw new ServiceException(sysApp.getAppId() + "已存在!");
            }
        } else {
            count = jdbcTemplate.queryForObject("select count(*) from sys_app where  app_id = ?", Integer.class, sysApp.getAppId().trim());
            if (count > 0) {
                throw new ServiceException(sysApp.getAppId() + "已存在!");
            }
        }
        if (sysApp.getId() == null) {
            sysApp.setId(Seq.getSeqId());
        }
        if (StringUtils.isNotEmpty(sysApp.getIconList())) {
            List<Long> fileIdList = sysApp.getIconList().stream().map(File::getFileId).collect(Collectors.toList());
            if (!fileService.saveFile(sysApp.getId(), 39, fileIdList)) {
                throw new ServiceException("保存app应用图标失败,请联系管理员");
            }
        }
        if (StringUtils.isNotEmpty(sysApp.getScreenshotList())) {
            List<Long> screenShotIdList = sysApp.getScreenshotList().stream().map(File::getFileId).collect(Collectors.toList());
            if (!fileService.saveFile(sysApp.getId(), 40, screenShotIdList)) {
                throw new ServiceException("保存app应用截图失败,请联系管理员");
            }
        }
    }

    @Override
    @Transactional
    public int deleteSysAppByIds(Long[] ids) {
        appVersionMapper.deleteSysAppVersionByIds(ids);
        return sysAppMapper.deleteSysAppByIds(ids);
    }

    @Override
    public int deleteSysAppById(Long id) {
        return sysAppMapper.deleteSysAppById(id);
    }
}
